package com.yantong.controller.aftersale;

import com.yantong.annotation.Log;
import com.yantong.controller.BaseController;
import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.WorkClass;
import com.yantong.service.WorkClassService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author kanghaixin
 * @date 2018/9/3.11:18
 */
@Controller
@RequestMapping("/sales/orderType")
public class WorkClassController extends BaseController {

    private static final String prefix = "workorder/type";
    @Autowired
    private WorkClassService workClassService;

    /**
     * 售后工单类型列表
     * @return
     */
    @GetMapping()
    @RequiresPermissions("sales:orderType:orderType")
    public String orderTypes() {
        return prefix + "/orderType";
    }

    /**
     * 工单类型列表数据
     * @param offset
     * @param limit
     * @return
     */
    @ResponseBody
    @RequiresPermissions("sales:orderType:orderType")
    @GetMapping("/list")
    public PageUtils<WorkClass> listOrderTypes(int offset, int limit) {
        return workClassService.listOrderTypes(offset, limit);
    }


    /**
     * 添加工单类型页面
     * @return
     */
    @RequiresPermissions("sales:orderType:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增工单类型
     * @param workClass
     * @return
     */
    @Log(module = "工单类型管理", operation = "新增工单类型")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("sales:orderType:add")
    public ResultVo save(WorkClass workClass) {
        if (workClass == null) {
            return ResultVo.error();
        }
        if (workClassService.save(workClass)< 1){
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 工单类型编辑页面
     * @param classId
     * @param model
     * @return
     */
    @GetMapping("/edit/{classId}")
    @RequiresPermissions("sales:orderType:edit")
    public String edit(@PathVariable Long classId, Model model) {
        model.addAttribute("workClass", workClassService.getByKey(classId));
        return prefix +"/edit";
    }

    /**
     * 工单类型更新
     * @param workClass
     * @return
     */
    @Log(module = "工单类型管理", operation = "编辑工单类型")
    @ResponseBody
    @RequiresPermissions("sales:orderType:edit")
    @PostMapping("/update")
    public ResultVo update(WorkClass workClass) {
        if (workClass == null || workClass.getId() == null) {
            return ResultVo.error();
        }
        if (workClassService.update(workClass)< 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 工单类型删除操作
     * @param classId
     * @return
     */
    @Log(module = "工单类型管理", operation = "删除工单类型")
    @ResponseBody
    @RequiresPermissions("sales:orderType:remove")
    @PostMapping("/remove")
    public ResultVo remove(Long classId) {
        if (classId == null) {
            return ResultVo.error();
        }
        WorkClass workClass = new WorkClass();
        workClass.setId(classId);
        workClass.setIsdelete(1);
        if (workClassService.update(workClass)< 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }
}
